/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.farent.solutions.plugins.stripes.hyperstripes.core.tags;

import com.farent.solutions.plugins.stripes.hyperstripes.core.Attribute;
import com.farent.solutions.plugins.stripes.hyperstripes.core.ComponentModel;
import com.farent.solutions.plugins.stripes.hyperstripes.core.ComponentProcessor;
import com.farent.solutions.plugins.stripes.hyperstripes.core.ComponentTagHandler;
import com.farent.solutions.plugins.stripes.hyperstripes.core.FormChildComponentModel;
import com.farent.solutions.plugins.stripes.hyperstripes.core.FormMode;
import com.farent.solutions.plugins.stripes.hyperstripes.core.TagHandler;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.mvel2.templates.CompiledTemplate;
import org.mvel2.templates.TemplateCompiler;
import org.mvel2.templates.TemplateRuntime;

/**
 *
 * @author user
 */
@TagHandler("FormTable")
public class FormTableTagHandler extends FormTagHandler implements ComponentTagHandler {

    @Override
    public String processContent(ComponentModel model, Object bindings) {
        FormComponentModel formModel = (FormComponentModel) model;
        formModel.addAttribute(new Attribute("class", "hs-form"));
        FormMode mode = parseFormMode(formModel.getAttributes(), bindings);
        formModel.setMode(mode);
        List<ComponentModel> children = formModel.getChildren();
        CompiledTemplate compiled3 = null;
        StringBuilder prettyBuilder = new StringBuilder();
        for (int i = 0; i < children.size(); i++) {
            FormChildComponentModel child = (FormChildComponentModel) children.get(i);
            child.setMode(mode);
            String childString = ComponentProcessor.processComponentContent(child, bindings);
            prettyBuilder.append(childString);
        }
        Map variables = new HashMap();
        variables.put("content", prettyBuilder.toString());
        variables.put("model", formModel);
        InputStream template3 = InputTagHandler.class.getResourceAsStream("formTable.tag");
        compiled3 = TemplateCompiler.compileTemplate(template3);
        return (String) TemplateRuntime.execute(compiled3, variables);
    }

    @Override
    public void createContent(ComponentModel model, Object bindings, OutputStream outputStream) {
        FormComponentModel formModel = (FormComponentModel) model;
        formModel.addAttribute(new Attribute("class", "hs-form"));
        FormMode mode = parseFormMode(formModel.getAttributes(), bindings);
        formModel.setMode(mode);
        List<ComponentModel> children = formModel.getChildren();
        CompiledTemplate compiled3 = null;
        StringBuilder prettyBuilder = new StringBuilder();
        for (int i = 0; i < children.size(); i++) {
            FormChildComponentModel child = (FormChildComponentModel) children.get(i);
            child.setMode(mode);
            String childString = ComponentProcessor.processComponentContent(child, bindings);
            prettyBuilder.append(childString);
        }
        Map variables = new HashMap();
        variables.put("content", prettyBuilder.toString());
        variables.put("model", formModel);
        InputStream template3 = InputTagHandler.class.getResourceAsStream("formTable.tag");
        compiled3 = TemplateCompiler.compileTemplate(template3);
        TemplateRuntime.execute(compiled3, variables, outputStream);
    }
}
